<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 模板管理
 */
namespace app\allwin\controller\info;
use app\allwin\controller\Common;
use app\allwin\model\AllwinInfoTpl;
use app\allwin\model\AllwinInfoCate;

class Tpl extends Common{


    public function initialize() {
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'模板库','url'=>url("info.tpl/index")]]);
    }

    //商品规格
    public function index(){
        $view['lists'] = AllwinInfoTpl::where($this->mini_program)->order('id asc')->paginate(20);
        $view['tabs']    = [
            ['name' =>'主题管理','url' =>url('info.cate/index')],
            ['name' =>'模板库','url' =>url('info.tpl/index'),'action' => 1],
            ['name' =>'推广设置','url' =>url('setting/info')],
        ];
        return view('index',$view);
    }

    //规格编辑
    public function edit(){
        if(request()->isAjax()){
            $data = [
                'id'                => $this->request->param('id/d',0),
                'name'              => $this->request->param('name/s'),
                'note'              => $this->request->param('note/s'),
                'title'             => $this->request->param('title/a'),
                'types'             => $this->request->param('types/a'),
                'values'            => $this->request->param('values/a'),
                'placeholder'       => $this->request->param('placeholder/a'),
                'member_miniapp_id' => $this->member_miniapp_id,
            ];
            $validate = $this->validate($data,'Infotpl.edit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            return AllwinInfoTpl::edit($data);
        }else{
            $id = $this->request->param('id/d',0);
            if($id){
                $info = AllwinInfoTpl::where($this->mini_program)->where(['id' => $id])->find();
                $fields = json_decode($info->fields,true);
                foreach ($fields as $key => $value) {
                    if($value['types'] == 'selector'){
                        $fields[$key]['values'] = implode('|',$value['values']);
                    }
                }
                $info->fields = $fields;
                $view['info'] = $info;
                return view()->assign($view);
            }else{
                return view('add');
            }
        }
    }

    //规格编辑
    public function users(){
        if(request()->isAjax()){
            $data = [
                'id'                => $this->request->param('id/d',0),
                'is_shop'           => $this->request->param('is_shop/d',0),
                'name'              => $this->request->param('button_name/s','下单'),
                'tips'              => $this->request->param('tips/s','下单'),
                'title'             => $this->request->param('title/a'),
                'types'             => $this->request->param('types/a'),
                'values'            => $this->request->param('values/a'),
                'placeholder'       => $this->request->param('placeholder/a'),
                'member_miniapp_id' => $this->member_miniapp_id,
            ];
            $validate = $this->validate($data,'Infotpl.edit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            return AllwinInfoTpl::usersEdit($data);
        }else{
            $id = $this->request->param('id/d',0);
            $info = AllwinInfoTpl::where($this->mini_program)->where(['id' => $id])->find();
            $users = json_decode($info->users,true);
            foreach ($users as $key => $value) {
                if($value['types'] == 'selector'){
                    $users[$key]['values'] = implode('|',$value['values']);
                }
            }
            $info->users  = $users;
            $view['info'] = $info;
            return view()->assign($view);
        }
    }

    //规格模板
    public function delete(int $id){
        $rel = AllwinInfoCate::where(['tpl_id' => $id])->find();
        if($rel){
            return enjson(0,'模板在【'.$rel->name.'】的信息类别中启用,禁止删除');
        }
        $result = AllwinInfoTpl::destroy(['member_miniapp_id' => $this->member_miniapp_id,'id' => $id]);
        if($result){
            return enjson(200,'操作成功');
        }
        return enjson(0);
    }
}